// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:

import Card from "./Card";

//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
const indian_names = [
    "Aarav", "Vivaan", "Aditya", "Vihaan", "Arjun", "Reyansh", "Muhammad", "Sai", "Arnav", "Ayaan",
    "Krishna", "Ishaan", "Shaurya", "Atharva", "Advait", "Pranav", "Advaith", "Aaryan", "Dhruv", "Kabir",
    "Ritvik", "Aarush", "Kian", "Darsh", "Veer", "Raj", "Mohammed", "Aryan", "Veer", "Samarth",
    "Yuvan", "Aarav", "Rudra", "Reyansh", "Siddharth", "Aaditya", "Sarvesh", "Ayaansh", "Krish", "Naksh",
    "Armaan", "Aadvik", "Ansh", "Ishaan", "Rohan", "Arhaan", "Dev", "Arjun", "Aarush", "Vivaan",
    "Adhiraj", "Anay", "Daksh", "Aarav", "Vihaan", "Arnav", "Ishaan", "Aarush", "Krishna", "Sai",
    "Arjun", "Shaurya", "Atharv", "Ayaan", "Aryan", "Rohan", "Ansh", "Aaditya", "Veer", "Samarth",
    "Rudra", "Sarvesh", "Reyansh", "Kabir", "Mohammed", "Arhaan", "Dhruv", "Kian", "Yuvan", "Raj",
    "Advait", "Ritvik", "Darsh", "Pranav", "Aadvik", "Aaryan", "Dev", "Naksh", "Armaan", "Ayaansh"
]
export enum PlayerType {
    Player = 1,
    Computer = 2
}
const {ccclass, property} = cc._decorator;

@ccclass
export default class PlayerCtrl extends cc.Component {

    @property(cc.SpriteAtlas)
    headIconAtlas: cc.SpriteAtlas = null;
    @property(cc.Sprite)
    headIcon: cc.Sprite = null;
    @property(cc.Label)
    playerName: cc.Label = null;

    @property(cc.Node)
    handCards: cc.Node = null;

    @property(cc.Label)
    playerCoin: cc.Label = null;

    playerCoinNum: number = 0;

    @property({type: cc.Enum(PlayerType)})
    playerType: PlayerType = PlayerType.Player;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let headCount = this.headIconAtlas.getSpriteFrames().length;
        if(this.playerType == PlayerType.Computer){
            this.playerCoinNum = Math.random() * 1000000 + 1000000;
            this.playerName.string = indian_names[Math.floor(Math.random() * indian_names.length)]
            this.headIcon.spriteFrame = this.headIconAtlas.getSpriteFrames()[Math.floor(Math.random() * headCount)];
        }else{
            this.playerName.string = "Dawei"
            let money = cc.sys.localStorage.getItem('money');
            if(money){
                this.playerCoinNum = parseInt(money);
            }else{
                this.playerCoinNum = 1000000;
            }
            
        }
        this.playerCoin.string = this.playerCoinNum.toFixed(0)
    }

    start () {

    }

    activeCardsClickEvent(){
        this.handCards.children.forEach((item) => {
            item.getComponent(Card).enableCardClickEvent();
        })
    }
    disableCardsClickEvent(){
        this.handCards.children.forEach((item) => {
            item.getComponent(Card).disableCardClickEvent();
        })
    }
    // update (dt) {}
}
